package com.presto.directo.mvvm.view

import android.content.Intent
import android.os.Bundle
import android.view.*
import androidx.core.os.bundleOf
import androidx.core.view.*
import androidx.fragment.app.Fragment
import com.presto.directo.R
import com.presto.directo.base.BaseFragment
import com.presto.directo.databinding.FragmentRepayBinding
import com.presto.directo.entity.*
import com.presto.directo.net.*
import com.presto.directo.utils.*

class RepayFragment : BaseFragment() {

    private val binding by lazy { FragmentRepayBinding.inflate(layoutInflater) }
    private var loanStatus: LoanStatusEntity? = null
    private var extensionEntity: ExtensionEntity? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        loanStatus = arguments?.getSerializable("loanStatus") as LoanStatusEntity?
        loanStatus?.run {
            binding.tvOrder.text = getString(R.string.text_order_title) + busiId
            binding.tvRepayAmount.text = paymentAmount.appendAmountUnit()
            if (loan_status == "3") {
                binding.tvTitle.text = getString(R.string.text_repay_title)
                binding.tvTimeTip.text = getString(R.string.text_deadline)
                binding.tvRepayTime.text = deadline
                binding.ivOverdue.isInvisible = true
                binding.llManageText.isVisible = false
                binding.llManageValue.isVisible = false
                binding.tvTip.isVisible = true
            } else {
                binding.tvTitle.text = getString(R.string.text_overdue_title)
                binding.tvTimeTip.text = getString(R.string.text_overdue_day)
                binding.tvRepayTime.text = penaltyDays.appendDayUnit()
                binding.ivOverdue.isInvisible = false
                binding.llManageText.isVisible = manager_fee.isNotEmpty()
                binding.llManageValue.isVisible = manager_fee.isNotEmpty()
                binding.tvManageAmount.text = manager_fee.appendAmountUnit()
                binding.tvTip.isVisible = false
            }
            binding.tvApplyAmount.text = principal.appendAmountUnit()
            binding.tvInterestAmount.text = interest.appendAmountUnit()
            binding.tvApplyTime.text = app_time
            binding.tvPenaltyAmount.text = penalty.appendAmountUnit()
        }
        start(onNext = {
            getApiService().getPersonInfo(getHttpMap().sign(22)).result?.let {
                binding.tvUserName.text = getString(R.string.text_name_loan_user) + it.real_name
            }
        }, toast = false)
    }

    override fun initEvent() {
        binding.btnRepay.setOnClickListener {
            Af.click_repay.track(mActivity)
            val intent = Intent(mActivity, RepayActivity::class.java)
            intent.putExtra("busiId", loanStatus?.busiId)
            intent.putExtra("amount", loanStatus?.remainAmount)
            startActivity(intent)
        }
        binding.btnRepayExtension.setOnClickListener {
            if (extensionEntity == null) return@setOnClickListener
            val intent = Intent(mActivity, RepayActivity::class.java)
            intent.putExtra("busiId", loanStatus?.busiId)
            intent.putExtra("extension", extensionEntity)
            startActivity(intent)
        }
    }

    companion object {
        fun createFragment(loanStatus: LoanStatusEntity?): Fragment {
            val fragment = RepayFragment()
            val bundle = bundleOf("loanStatus" to loanStatus)
            fragment.arguments = bundle
            return fragment
        }
    }
}